<template>
    <div class="task-rule">
        <navbar :title="$t('user_day_task')"></navbar>
        <div class="content-noBottom">
            <div class="task-rule-body  another-content">
            <div class="task-rule-item" v-for="(item,index) in taskRuleList" :key="index" @click="toView(item.taskType)">
                <div class="rule-title">
                    {{ $t('task_' + item.taskType) }}
                </div>
                <div class="rule-point">
                    +{{ item.integral }}{{ $t('task_integral') }}
                </div>
                <div class="rule-bar">
                    <div class="rule-btn" v-if="!item.userTaskId && !item.acquireDatetime">
                        {{ $t('task_not_ok') }}
                    </div>
                    <div class="rule-btn task-get" @click.stop="userAcquireIntegral(item)" v-else-if="item.userTaskId && !item.acquireDatetime">
                        {{ $t('task_get_integral') }}
                    </div>
                    <div class="rule-btn task-ok" v-else>
                        {{ $t('task_ok') }}
                    </div>
                </div>
            </div>
        </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'UserTaskList',
    data() {
        return {
            taskRuleList: [], //日常任务规则列表
            errorMsg: '', //提示信息
            userEntId: null, //用户entId
            taskRule: {} //任务跳转地址
        };
    },
    created() {
        this.userEntId = this.$route.params.userEntId;
        if (this.userEntId) {
            this.taskRule = {
                'complete_course': '/learn/myCourses/' + this.userEntId, //完成课程
                'browse_an_open_class': '/learn/myCourses/' + this.userEntId + '?type=AUDIOVIDEO', //浏览一个公开课
                'work_browse': '/circle/home', //浏览一个工作圈
                'work_publish_topic': '/circle/home', //在工作圈发表一个话题
                'work_publish_knowledge': '/circle/home', //在工作圈分享一个知识
                'curriculum_review': '/learn/myCourses/' + this.userEntId, //对课程进行评论
                'complete_exam': '/learn/examination', //完成考试
                'online_question': '/know/knowList', //在线提问
                'online_answer': '/know/knowList', //在线回答问题
                'browse_knowledge': '/learn/knowledge', //浏览知识
                'knowledge_sharing': '/learn/knowledge' //知识分享（上传就算，不需要审核通过）
            };
        }
        this.findTaskRuleList();
    },
    methods: {
        /**
         * 查询任务列表
         */
        findTaskRuleList() {
            if (this.userEntId) {
                this.$axios.get(`/app/integral/task/complete`).then(res => {
                    if (res && res.data) {
                        this.taskRuleList = res.data;
                    }
                });
            }
        },
        /**
         * 领取积分
         * @param userTaskId
         */
        userAcquireIntegral(taskRule) {
            this.$axios.post(`/app/user-task/${taskRule.userTaskId}`).then(res => {
                taskRule.acquireDatetime = new Date();
                this.errorMsg = this.$t('task_msg_get_integral_ok');
                this.$toast(this.errorMsg);
            }).catch(res => {
                if (res.errMsg === 'ERROR_USER_TASK_NOT_OK') {
                    this.errorMsg = this.$t('task_msg_get_integral_error_not');
                } else if (res.errMsg === 'ERROR_USER_TASK_TET_ACQUIRE') {
                    this.errorMsg = this.$t('task_msg_get_integral_error_tet');
                } else {
                    this.errorMsg = this.$t('task_msg_get_integral_error');
                }
                this.$toast(this.errorMsg);
            });
        },
        /**
         * 跳转
         * @param taskType
         */
        toView(taskType) {
            if (this.taskRule[taskType]) {
                this.$router.push({path: this.taskRule[taskType], query: {type: 'AUDIOVIDEO'}});
            }
        }
    }
};
</script>

<style lang="less" scoped>
    .task-rule{
        .task-rule-body{
            position: absolute;
            top: 23px;
            width: 100%;
            .task-rule-item{
                background: #fff;
                height: 60px;
                /*margin-bottom: 10px;*/
                display: flex;
                align-items: center;
                padding: 0 10px;
                border-bottom: 1px solid #ccc;
                .rule-title{
                    flex: 2;
                }
                .rule-point{
                    flex: 1;
                    text-align: center;
                }
                .rule-bar{
                    flex: 1;
                    display: flex;
                    justify-content: flex-end;
                    .rule-btn{
                        width: 90%;
                        height: 30px;
                        border-radius: 40px;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        border: 1px solid darkorange;
                    }
                    .task-get {
                        background: darkorange;
                        color: #fff;
                    }
                    .task-ok {
                        color: #ccc;
                        border: 1px solid #ccc;
                    }
                }
            }
        }
    }
</style>
